<?php
namespace app\admin\controller;

use app\repositories\contracts\MenuInterface;
use think\App;
use think\facade\View;
use app\Request;
use app\repositories\contracts\AdminLogInterface;
use app\api\service\serv\ApiLogService;

class Index extends ViewBaseController
{

    public function index(AdminLogInterface $adminLog,ApiLogService $apiLog)
    {
        $adminLogWeekDatas = self::getAdminLogWeekData($adminLog);
        $apiLogWeekDatas = self::getApiLogWeekData($apiLog);
        //halt(json_encode($adminLogWeekDatas));
        return View::fetch('index',[
            'adminLogCount'=>json_encode($adminLogWeekDatas['count']),
            'adminLogDate'=>json_encode($adminLogWeekDatas['date']),
            'apiLogCount'=>json_encode($apiLogWeekDatas['count']),
            'apiLogDate'=>json_encode($apiLogWeekDatas['date'])
        ]);
    }

    public function clearCache(Request $request)
    {
        //这个清除的是admin应用
        //$path = runtime_path();
        //这个清除所有应用的缓存
        $path = root_path().'runtime';
//        halt($path);
        delFileByDir($path);
        //返回上一页
        return redirect($request->header('referer'));
    }

    public function getAdminLogWeekData($adminLog)
    {
        //获取本周接口日志请求量
        $weekdata = $adminLog->getAdminLogListCountWhere('week');
        //将所有日期改为日
        foreach ($weekdata as $k => $v)
        {
            $day = date('d',strtotime($v['create_time']));
            $weekdata[$k]['create_time'] = $day;
        }
        //将数组按照日进行分组
        $weekdata = array_group_by($weekdata,'create_time');
        //获取每日有多少条数据
        foreach ($weekdata as $day => $data)
        {
            $weekdata[$day] = count($weekdata[$day]);
        }
        //将kv对转为echarts需要的数据格式
        $weekdatasCount =[];
        $weekdatasDate = [];
        foreach ($weekdata as $day => $count)
        {
            $weekdatasCount[] = $count;
            $weekdatasDate[] = $day;
        }
        $weekdatas = [
            'count' => $weekdatasCount,
            'date' => $weekdatasDate
        ];
        return $weekdatas;
    }

    public function getApiLogWeekData($apiLog)
    {
        //获取本周接口日志请求量
        $weekdata = $apiLog->getApiLogListCountWhere('week');
        //将所有日期改为日
        foreach ($weekdata as $k => $v)
        {
            $day = date('d',strtotime($v['create_time']));
            $weekdata[$k]['create_time'] = $day;
        }
        //将数组按照日进行分组
        $weekdata = array_group_by($weekdata,'create_time');
        //获取每日有多少条数据
        foreach ($weekdata as $day => $data)
        {
            $weekdata[$day] = count($weekdata[$day]);
        }
        //将kv对转为echarts需要的数据格式
        $weekdatasCount =[];
        $weekdatasDate = [];
        foreach ($weekdata as $day => $count)
        {
            $weekdatasCount[] = $count;
            $weekdatasDate[] = $day;
        }
        $weekdatas = [
            'count' => $weekdatasCount,
            'date' => $weekdatasDate
        ];
        return $weekdatas;
    }

}
